package fsoft.com.vn.directory.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import fsoft.com.vn.directory.form.GovOfficeRegion;
import fsoft.com.vn.directory.service.GovOfficeRegionService;

@Controller
public class GovOfficeRegionController {

	@Autowired
	private GovOfficeRegionService govService;

	@RequestMapping("/Govs")
	public String listGov(Map<String, Object> map, HttpServletRequest request) {
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		int check = ServletRequestUtils.getIntParameter(request, "check", 0);
		String split = ServletRequestUtils.getStringParameter(request, "split",
				"");
		map.put("govsplit", new GovOfficeRegion());
		map.put("check", check);
		map.put("page", page);
		map.put("split", split);
		map.put("govList", govService.listGov(page, split, check).get(1));
		map.put("size", govService.listGov(page, split, check).get(0));
		String xxxx = govService.listGov(page, split, check).get(0).toString();
		System.out.println("page is " + xxxx);
		if (page > Integer.parseInt(xxxx) && page > 1){
			map.put("page",1);
			return "redirect:./Govs";
		}
		else
			return "govofficeregion";
	}

	@RequestMapping("/viewdetailgov/{id}")
	public String viewDetail(Map<String, Object> map,
			@PathVariable("id") String id) {
		GovOfficeRegion gov = govService.viewDetail(id);
		map.put("gov", new GovOfficeRegion());
		map.put("govdetail", gov);
		return "viewdetailgov";
	}
}
